<?php
namespace App\Repositories\Admin;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\CategoryModel;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCheckModel;
use App\Models\Admin\OrderTempModel;
use App\Models\User\UserModel;
use App\Models\User\UserOrdersModel;
use App\Repositories\BaseRepository;

class OrderBaleRepository extends BaseRepository
{
    protected $repository;
    protected $user;

    public function __construct()
    {
        $this->repository = new OrderBaleModel();
        $this->user = new UserModel();
    }

    /**
     * 操作分页获取
     */
    public function getAdminIndex()
    {
        $models = $this->repository
            ->orderBy('id','desc')
            ->paginate(10);
        $models->limit = 10;
        return $models;
    }

    /**
     * 获取web首页货单
     */
    public function getWebIndex()
    {
        return $this->repository
            ->where('status','<',3)
            ->skip(0)
            ->take(4)
            ->orderBy('id','desc')
            ->get();
    }

    /**
     * 获取web货单搜索条件
     */
    public function getWebConditions()
    {
        //加工件数
        $nums = [
            [ 'numId' => '1', 'numName' => '不限', ],
            [ 'numId' => '2', 'numName' => '1-500件', ],
            [ 'numId' => '3', 'numName' => '501-1000件', ],
            [ 'numId' => '4', 'numName' => '1千件以上', ],
        ];
        //工期时长
        $periods = [
            [ 'periodId' => '1', 'periodName' => '不限', ],
            [ 'periodId' => '2', 'periodName' => '一周内', ],
            [ 'periodId' => '3', 'periodName' => '一个月内', ],
            [ 'periodId' => '4', 'periodName' => '三个月内', ],
            [ 'periodId' => '5', 'periodName' => '半年以上', ],
        ];
        //加工单价
        $moneys = [
            [ 'moneyId' => '1', 'moneyName' => '不限', ],
            [ 'moneyId' => '2', 'moneyName' => '10元内', ],
            [ 'moneyId' => '3', 'moneyName' => '10-50元', ],
            [ 'moneyId' => '4', 'moneyName' => '100元以上', ],
        ];
        return [
            'nums' => $nums,
            'periods' => $periods,
            'moneys' => $moneys,
        ];
    }

    /**
     * 获取web货单列表
     */
    public function getWebBaleList($num,$period,$money,$page=1)
    {
        $query = $this->repository
            ->where('status','<',3)
            //抢单中的订单
            ->where('status','<',3);
        //件数
        if ($num==2) {
            $query = $query->whereBetween('num',[1,500]);
        } else if ($num==3) {
            $query = $query->whereBetween('num',[501,1000]);
        } else if ($num==4) {
            $query = $query->where('num','>',1000);
        } else {
        }
        //有效期
        if ($period==2) {
            $toTime = 3600 * 24 * 7;  //一周
            $query = $query->where('period','<',time() + $toTime);
        } else if ($period==3) {
            $toTime = 3600 * 24 * 30;  //一月
            $query = $query->where('period','<',time() + $toTime);
        } else if ($period==4) {
            $toTime = 3600 * 24 * 30 * 3;  //三月
            $query = $query->where('period','<',time() + $toTime);
        } else if ($period==5) {
            $toTime = 3600 * 24 * 30 * 6;  //六月
            $query = $query->where('period','<',time() + $toTime);
        } else {
        }
        //单价
        if ($money==2) {
            $query = $query->where('money','<',10);
        } else if ($money==3) {
            $query = $query->whereBetween('money',[10,50]);
        } else if ($money==4) {
            $query = $query->where('money','>',100);
        } else {
        }
        $pageStart = ($page - 1) * 10;
        $models = $query->skip($pageStart)
            ->take(10)
            ->orderBy('id','desc')
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $userModel = $this->user->where('id',$model->uid)->first();
            $head = $userModel ? $userModel->head : '';
            $dataArr[] = [
                'comName' => $model->com_name,
                'address' => $model->address,
                'num' => $model->num,
                'period' => '有效至：'.date('Y年m月d日',$model->period),
                'moneyTotal' => $model->money * $model->num .'元',
                'bigImg' => $head,
            ];
        }
        return $dataArr;
    }

    public function getIndexByFacStatus($uid=0)
    {
        $query = $this->repository->where('status','<',3);
        if ($uid) {
            $query = $query->where('uid',$uid);
        }
        return $query->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 通过公司ID，获取
     */
    public function getIndexByUid($uid)
    {
        return $this->repository->where('uid',$uid)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 通过公司ID，APP抢单列表获取
     */
    public function getIndexByCid($uid,$cateId)
    {
        $query = $this->repository->where('uid',$uid);
        if ($cateId) { $query = $query->where('cate',$cateId); }
        return $query->where('status','<',3)
            ->where('period','>',time())
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 通过公司ID，APP抢单列表获取
     */
    public function getOrderBaleByCid($cateId,$offset,$pageSize)
    {

        return $this->repository
            ->select('id','name','img','is_rob','period')
            ->where('period','>',time())
            ->where('status','=',OrderBaleModel::status_kaishi)
            ->where('visible','=',OrderBaleModel::visible_show)
            ->where('cate',$cateId)
            ->where('del',0)
            ->orderBy('id','DESC')
            ->skip($offset*$pageSize)
            ->take($pageSize)
            ->get();
    }

    /**
     * APP通过CID，获取某个企业货单
     */
    public function getAppBalesByCid($uid,$page)
    {
       // exit('aaaaaaaaaaaffffffff');
        $models = $this->repository
            ->where('uid',$uid)
            ->where('del',0)
            ->where('created_at','!=',0)
            ->orderBy('id','DESC')
            ->skip($page*10)
            ->take(10)
            ->get();
        $uid=OA::$user->getId();
        $dataArr = array();
       // print_r($models);exit;
        if (!count($models)) { return $dataArr; }
        foreach ($models as $key => $model) {
            //$userinfo=UserModel::getinfo($model->uid);
            $dataArr[$key]['id'] = $model->id;
            $dataArr[$key]['img'] =Fun::pImg($model->img);
            $dataArr[$key]['name'] =Fun::encode($model->name);
            $dataArr[$key]['created_at'] = Fun::date($model->created_at);
            $dataArr[$key]['updated_at'] = Fun::date($model->updated_at);
            $dataArr[$key]['status'] =$model->status;
            $dataArr[$key]['status_src'] =OrderBaleModel::getStatus($model->status);
            $dataArr[$key]['period'] =  Fun::date($model->period,false);
            $dataArr[$key]['detail']=Fun::shenglue($model->detail);

            $dataArr[$key]['cate'] =   CategoryModel::getName($model->cate);
            if($model->is_rob==OrderBaleModel::is_rob_putong){
                $dataArr[$key]['bond'] = '普通单';
            }else{
                $dataArr[$key]['bond'] =  '¥'.$model->bond.'元';
            }



            $dataArr[$key]['count'] =OrderTempModel::where([
                'bale'=>$model->id
            ])->count();
            $dataArr[$key]['uid']=$model->uid;
            $dataArr[$key]['visible']=$model->visible;
            $dataArr[$key]['isme'] = $model->uid==$uid;

        }
        return $dataArr;
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    public function getShowOne($id)
    {
        $model = $this->repository->where('id',$id)->first();
        return $model;
    }

    /**
     * 通过公司ID，获取
     */
    public function getShowOneByCid($cid)
    {
        return $this->repository->where('cid',$cid)->first();
    }

    /**
     * 通过分类，分页获取
     */
    public function getCategoryOrders($cid)
    {
        $models = $this->repository->select('name','img')->where('cate',$cid)->where('status','<',3)->orderBy('created_at','desc')->paginate(10);
        return $models;
    }

    /**
     * 通过id,获取详情
     */
    public function getCategoryOrdersDetail($id)
    {
        $models = $this->repository->where('id',$id)->first();
        return $models;
    }

    /**
     * 后台货单详情获取
     */
    public function getAdminShow($id)
    {
        return $this->repository
            ->where('id',$id)
            ->first();
    }

    /**
     * 后台货单详情设置
     */
    public function setAdminById($id,$data)
    {
        return $this->repository
            ->where('id',$id)
            ->update($data);
    }

    /**
     * 设置用户删除
     */
    public function setDel($id)
    {
        return $this->repository
            ->where('id',$id)
            ->where('del',0)
            ->update(['del'=>1]);
    }

    /**
     * 尺寸颜色中的尺寸
     */
    public function getSizelistSizes()
    {
        return array(
            'S', 'M', 'L', 'XL', 'XXL', 'XXXL',
        );
    }

    /**
     * 尺寸颜色中的颜色
     */
    public function getSizelistColors()
    {
        return array(
            '白', '红', '蓝',
        );
    }

    /**
     * 清空订单数据表
     */
    public function setClean()
    {
        $resBale = OrderBaleModel::truncate();
        if (!$resBale) { return -1; }
        $resTemp = OrderTempModel::truncate();
        if (!$resTemp) { return -2; }
        $resOrder = UserOrdersModel::truncate();
        if (!$resOrder) { return -3; }
        $resOrderCheck = OrderCheckModel::truncate();
        if (!$resOrderCheck) { return -4; }
        return 0;
    }
}